<?php

/**
 * General actions.
 *
 * @package    Redsea20
 * @subpackage Profile
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class generalActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
   public function executeLoadAttribute(sfWebRequest $request)
  {

 $params = "";// "TD1";
 
    if ($this->getRequestParameter('search') !=null && $this->getRequestParameter('search') != "")
    {
        $params=$this->getRequestParameter('search');
        
        $LocationAttribute=Doctrine_Query::create()
                  ->from("observationdepthattribute oda")
                  ->leftJoin("oda.Attribute a")
                  ->leftJoin("oda.Observationdepth od")
                  ->leftJoin("od.Observation o")
                  ->where('o.location = ?', $params)
                  ->groupBy("a.attributename")
                  ->execute();
				
    }
$tmCounter=0;
     foreach($LocationAttribute as $item){


$tmCounter++;
            if ($tmCounter==1)
         
         echo '<option  value="'.$item["attributeid"].'FLG_ATTRIBUTE_REPORT'.$item->Observationdepth->Observation["location"].'">'.$item->Attribute["attributename"] .' </option>';
            else
         echo '<option value="'.$item["attributeid"].'FLG_ATTRIBUTE_REPORT'.$item->Observationdepth->Observation["location"].'">'.$item->Attribute["attributename"] .' </option>';
      
    }
    


    return sfView::NONE;
  }

  public function executeLoadAttributeReport(sfWebRequest $request)
  {

 $params = "";// "2_TD1";
    if ($this->getRequestParameter('search') !=null && $this->getRequestParameter('search') != "")
    {

        $t=explode("FLG_ATTRIBUTE_REPORT", $this->getRequestParameter('search'));

				if (count($t)>1)
				{
                list($AttributeId, $LocationVal) = $t;
                 $this->chartDetail=Doctrine_Query::create()
                  //->select('odd.id, od.location')
                  ->from('observationdepthattribute oda')
		  ->leftJoin('oda.Observationdepth od')
                  ->leftJoin('od.Observation o')
                  ->where('oda.attributeid =?', $AttributeId)
                  ->andwhere('o.location =?', $LocationVal)
                  ->orderby('oda.id')
                  ->execute();
				}



    }

    $dataHolder = array();
    $uniqueUnits = array();
    $uniqueYears = array();
    $uniqueDepths = array();
    $output = "";
    $speciesList = "";
    $unitList = "";
    $yearList = "";
    $depthList = "";
    $counterArray=0;
    foreach($this->chartDetail as $item){

        // Filtering out unique Observation Location from returned result using data holder
        //if (!array_key_exists($item->Observationdepth->Observation["id"], $dataHolder)){
        //    $dataHolder[$item->Observationdepth->Observation["id"]] = $item->Observationdepth->Observation["location"];
        //    $output = $output.$item->Observationdepth->Observation["id"].",".$item->Observationdepth->Observation["location"].":";
        //}
        if (!in_array($item->Observationdepth["id"], $dataHolder)){
            array_push($dataHolder, $item->Observationdepth["id"]);
            $counterArray++;
            $output =$output.$counterArray.",Depth ".$item->Observationdepth["observationdepth"].":";


        }


        $speciesList = $speciesList.$counterArray.",".$item->Attribute["attributename"]."(".$item->Attribute->Unit["unit"].")".",".((double)$item->getAttributevalue()).",".$item->Attribute["id"].":";

        // Filtering out unique Unit(s) from returned result
//        if (!in_array($item->Observationdepth["unitid"], $uniqueUnits)){
//            array_push( $uniqueUnits, $item->Observationdepth["unitid"]);
//            $unitList = $unitList.$item->Species->Unit["unit"].':';
//        }

        // Filtering out unique Year(s) from returned result
//        if (!in_array($item->Observationdepth->Observation["observationdate"], $uniqueYears)){
//            array_push( $uniqueYears, $item->Observationdepth->Observation["observationdate"]);
//            $yearList = $yearList.substr($item->Observationdepth->Observation["observationdate"],0,4).', ';
//        }
//
//        // Filtering out unique Depth(s) from returned result
//        if (!in_array($item->Observationdepth["id"], $uniqueDepths)){
//            array_push( $uniqueDepths, $item->Observationdepth["id"]);
//            $depthList = $depthList.$item->Observationdepth["observationdepth"].$item->Observationdepth->Unit["unit"].', ';
//        }
    }
    echo $output == "" ? $output : rtrim($output, ":").'|'.rtrim($speciesList, ":");

    return sfView::NONE;
  }





  /**
   * Draws the XML file for MARKERS
   * @param sfWebRequest $request
   * @return <type>
   */
  public function executeMap_Data(sfWebRequest $request)
  {
      $str="";
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {

          $str=$this->getRequestParameter('search');
            $this->observationList = Doctrine_Query::create()
                                ->select('o.lat,o.lng,o.location')
                                ->from('observation o')
                                ->leftJoin('o.Observationdepth od')
                                ->leftJoin('od.Observationdepthdetail odd')
                                ->where('odd.id In ('.$str.')')
                                ->execute();
      }
 else {
          $this->observationList = Doctrine_Query::create()
                                ->select('o.lat,o.lng,o.location')
                                ->from('observation o')
                                ->execute();
      }
        echo "<markers>";

        foreach($this->observationList as $r){
            echo "<marker ";
            $tag = "lat";
            echo ($tag. '="'.$r->getLat().'" ');
            $tag = "lng";
            echo ($tag. '="'.$r->getLng().'" ');
            $tag = "Location";
            echo ($tag. '="'.$r->getLocation().'" ');
            $tag = "str";
            echo ($tag. '="'.$str.'" ');
            echo (' />');
        }
        echo "</markers>";

        return sfView::NONE;
  }

public function executeOverlay_Data(sfWebRequest $request)
  {
      $str="";
      if ($this->getRequestParameter('search')!=null && $this->getRequestParameter('search')!="")
      {

          $str=$this->getRequestParameter('search');
            $this->overlayData = Doctrine_Query::create()
                  ->from('researchoverlay ro')
		  ->where('ro.id In ('.$str.')')
                  ->execute();
      }

        echo "<markers>";

        foreach($this->overlayData as $r){
            echo "<marker ";
            $tag = "id";
            echo ($tag. '="'.$r->getId().'" ');
            $tag = "nlat";
            echo ($tag. '="'.$r->getNlat().'" ');
            $tag = "wlng";
            echo ($tag. '="'.$r->getWlng().'" ');
            $tag = "slat";
            echo ($tag. '="'.$r->getSlat().'" ');
            $tag = "elng";
            echo ($tag. '="'.$r->getElng().'" ');
            echo (' />');
        }
        echo "</markers>";

        return sfView::NONE;
  }
  
 }